<?php
/**
 * User: lsx
 * Date: 2017/5/4
 * Time: 17:28
 * Progress:0%
 */

namespace logic\admin\index;

use model\ShopOrder;
use model\Account;
use model\ShopGoods;
use model\Admin;
use think\Config;
use think\Cookie;
use think\Db;

class LIndex
{
    public function index(){
        $model=new Admin();
        $cookie_name =  Config::get('const.cookie_name_admin');
        $cookie = Cookie::get($cookie_name);
        $data=$model->where(['id'=>$cookie])->find();
        return $data->getData();
    }

    public function change_password_save($id,$param){
        $model=new Admin();
        $admin=$model->where(['id'=>$id])->find();
        $admin_arr=$admin->getData();
        if($admin_arr['password'] != md5($param['old_password'])){
            return array(
                'success'=>false,
                'message'=>'原密码错误'
            );
        };
        if($param['new_password'] != $param['confirm_password']){
            return array(
                'success'=>false,
                'message'=>'两次密码不一致'
            );
        }
        $res=$model->save(['password'=>$param['new_password']],['id'=>$id]);
        if($res){
            $cookie_name =  Config::get('const.cookie_name_admin');
            Cookie::set($cookie_name,null);
            return array(
                'success'=>true,
                'message'=>'更改密码成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>'更改密码失败'
            );
        }

    }

    /**
     * 今日新增商品订单数量
     * @return int|string
     */
    public function today_add_order()
    {
        $m_shop_order = new ShopOrder();

        $start = strtotime(date('Y-m-d 00:00:00'));
        $end = strtotime(date('Y-m-d H:i:s'));
        $where['order_time'] = array('between',array($start,$end));

        $count = $m_shop_order->where($where)->count();

        return $count;
    }

    /**
     * 今日新增会员
     * @return int|string
     */
    public function today_add_shop_user(){
        $m_account = new Account();

        $start = strtotime(date('Y-m-d 00:00:00'));
        $end = strtotime(date('Y-m-d H:i:s'));
        $where['reg_time'] = array('between',array($start,$end));
        $count = $m_account->where($where)->count();

        return $count;
    }

    /**
     * 今日配送员
     * @return int|string
     */
    public function today_add_account_deliivery(){

        $start = strtotime(date('Y-m-d 00:00:00'));
        $end = strtotime(date('Y-m-d H:i:s'));
        $where['add_time'] = array('between',array($start,$end));
        $where['flag_delete']=0;
        $where['role_id']=2;
        $count = Db::name('account_role')->where($where)->count();

        return $count;
    }

    /**
     * 今日配送员
     * @return int|string
     */
    public function account_count(){
        $m_account = new Account();
        $where['flag_lock'] =0;
        $where['flag_delete'] =0;
        $count = $m_account->where($where)->count();
        return $count;
    }

    /**
     * 今日配送员
     * @return int|string
     */
    public function delivery_count(){
        $where['flag_delete'] =0;
        $where['role_id']=2;
        $count = Db::name('account_role')->where($where)->count();
        return $count;
    }

    /**
     * 商品总数
     * @return int|string
     */
    public function goods_count(){
        $sg=new ShopGoods();
        $where['flag_delete'] =0;
        $count = $sg->where($where)->count();
        return $count;
    }

    /**
     * 订单总数
     * @return int|string
     */
    public function order_count(){
        $model=new ShopOrder();
        $count = $model->count();
        return $count;
    }

}